import { wxRequest } from '../../utils/server.js';  
const _wxRequest = new wxRequest();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
     device:{ },
     typeIndex:0,
     deviceList:[], 
     typeId: '',
     uploadUrl: wx.getStorageSync('uploadUrl'),
  },
   
  onShow: function (options) { 
    this.setData({
      typeId: wx.getStorageSync('currentTypeId')
    })
    this.getDeviceList()
    
  },

   
//获取记录列表
dyeRecord:function(){       
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/dyeRecord/findReplaceList',
      method: 'POST',
      data:{
        devId: this.data.deviceList[this.data.typeIndex].id,
        pageNo: 1, 
        pageSize:50,
        keyword: this.data.keyword, 
      } 
    }, (res) => { 
      let list = res.list; 
      this.setData({
        recodeList: list, 
      });

      console.log(res)
      
    }) 
  },
 





  
  //触底时，加载更多
  onReachBottom: function () {
    if (this.data.hasNextPage) { 
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.dyeRecord();
    }
  },

  //搜索关键词
  seachInputConfirm: function (e) {    
    this.setData({
      pageNo: 1,
      keyword:e.detail.value
    })
    this.dyeRecord();
  },
  
  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.dyeRecord();
  },

  seachInputVal: function (e) { 
    var inputval = e.detail.value;
    console.log(e)
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function () {
    this.setData({
      keyword: ''
    })
    this.dyeRecord();
  },
 



//详情
  toDetail:function(e){
    var item= e.currentTarget.dataset.item
   wx.navigateTo({
     url: '/pages/color_residue/detail?id='+item.id,
   })
  },








  bindPickerChange(e){
    var val=e.detail.value
    this.setData({
      typeIndex:val
    },()=>{
      this.dyeRecord();
    })
  },
  
  
  
  
  
  
  getDeviceList: function() {
    var params = {
      keyword: this.data.keyword, 
      pageNo: 1,
      state: 2,
      typeId: this.data.typeId,
      pageSize:999
    }
     
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/device/findList',
      method: 'POST',
      data:params
    }, (res) => {
      let list = res.list.map(item => {
        if(item.stoName) {
          item.note += ` - ${item.stoName}`;
        }

        if(item.stoCreateName) {
          item.note += ` - ${item.stoCreateName}`;
        }
        return item;
      });
      
      this.setData({
        deviceList: list, 
      },()=>{
        if(list.length>0){
          this.dyeRecord() 
        }
        
      });
    })
  },




})